package com.taihua.dao;

import com.taihua.entity.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class AccountDaoImpl implements AccountDao{
    JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int addAccount(Account account) {

        return jdbcTemplate.update("insert into account(username,balance) values (?,?)",account.getUsername(),account.getBalance());
    }

    @Override
    public int updateAccount(Account account) {

        return jdbcTemplate.update("update account set username = ?,balance = ? where id = ?",account.getUsername(),account.getBalance(),account.getId());
    }

    @Override
    public int deleteAccountById(int id) {
        return jdbcTemplate.update("delete from account where id = ?",id);
    }

    @Override
    public Account queryAccountById(int id) {
        RowMapper<Account> mapper= new BeanPropertyRowMapper<>(Account.class);
        return jdbcTemplate.queryForObject("select * from account where id = ?",mapper,id);
    }

    @Override
    public List<Account> queryAllAccount() {
        RowMapper<Account> mapper= new BeanPropertyRowMapper<>(Account.class);
        return jdbcTemplate.query("select * from account",mapper);
    }

    //用jdbc做事务处理的回滚
    @Override
    public boolean transfer1(int outId, int inId, double balance) {
        String url="jdbc:mysql://localhost:3306/cloudlibrary?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
        String user="root";
        String pwd="123456";
        Connection connection=null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("加载驱动成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        try {
            connection = DriverManager.getConnection(url,user,pwd);
            System.out.println("连接成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println(throwables.getMessage());
        }

        PreparedStatement statement = null;
        try {
//            connection.setAutoCommit(false);//自动提交
            statement = connection.prepareStatement("update account set balance = balance - ? where id = ?");
            statement.setDouble(1,balance);
            statement.setInt(2,outId);
            boolean flag1 = statement.execute();
            System.out.println("flag1"+flag1);
//            int flag = 10/0;

            statement = connection.prepareStatement("update account set balance = balance - ? where id = ?");
            statement.setDouble(1,balance);
            statement.setInt(2, inId);
            boolean flag2 = statement.execute();
            System.out.println("flag2"+flag2);

            return flag1&&flag2;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                connection.close();
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }

        return false;
    }


    @Override
    public boolean transfer2(int outId, int inId, double balance) {
        int num1 = jdbcTemplate.update("update account set balance = balance - ? where id = ?",balance,outId);

        int num2 = jdbcTemplate.update("update account set balance = balance + ? where id = ?",balance,inId);
        return num1>0&&num2>0;
    }
}
